﻿using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using ProjectFootball.MVC.Models.RepositoryProviders;
using System;
using System.Linq;
using System.Web.Mvc;

namespace ProjectFootball.MVC.Areas.CompetitionArea.Controllers
{
    public class DetailsController : BaseController
    {
        #region Constructor
        public DetailsController(IRepositoryProvider provider)
            :base(provider)
        {
        }
        #endregion

        #region //CompetitionArea/Details
        public ActionResult Index(Guid id, DateTime? dt)
        {
            var competition = Provider.CompetitionRepository.SearchByPrimaryKey(id);

            if (competition == null)
                return RedirectHome;

            DateTime viewDate = dt != null ? (DateTime)dt : DateTime.Now;

            var competitionViewModel = competition.GetCompetitionViewModel(User, SearchType.Details);
            var campaign = Provider.CampaignRepository.GetCampaignByDate(id, viewDate);

            if (campaign != null)
            {
                var matches = Provider.MatchRepository.GetMatchesByDateRange(id, campaign.StartDate, campaign.EndDate).AsEnumerable();
                competitionViewModel.Matches = matches.OrderByDescending(m => m.MatchDate.Date).ThenBy(m => m.TeamVersion1.TeamName);
            }

            return View(competitionViewModel);
        }
        #endregion
    }
}
